package com.cxc.java8;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Random;
import java.util.function.Supplier;
import java.util.stream.Stream;

public class CreateStream {

    public static void main(String[] args) {
//        Stream<String> steamFromCollection = createSteamFromCollection();
//        steamFromCollection.forEach(System.out::println);
//        createSteamFromValues().forEach(System.out::println);
//        createSteamFromArrays().forEach(System.out::println);
//        createStreamFromFile();

        createObjStreamFromGenerate().forEach(System.out::println);
    }

    private static Stream<String> createSteamFromCollection(){
        return Arrays.asList("cxc", "text", "eee").stream();
    }
    private static Stream<String> createSteamFromValues(){
        return Stream.of("cxc", "text", "eee");
    }
    private static Stream<String> createSteamFromArrays(){
        String[]  strings =  {"cxc","teddt","dd"};
        return Arrays.stream(strings);
    }


    private static Stream<String> createStreamFromFile() {
        Path path = Paths.get("D:\\WORK_SPACE\\IDEA_WORK_SPACE\\cxc_java\\java8-share\\src\\main\\java\\com\\cxc\\java8\\Dish.java");
        try (Stream<String> streamFromFile = Files.lines(path)) {
            streamFromFile.forEach(System.out::println);
            return streamFromFile;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static Stream<Obj> createObjStreamFromGenerate() {
        return Stream.generate(new ObjSupplier()).limit(10);
    }

    static class ObjSupplier implements Supplier<Obj> {

        private int index = 0;

        private Random random = new Random(System.currentTimeMillis());

        @Override
        public Obj get() {
            index = random.nextInt(100);
            return new Obj(index, "Name->" + index);
        }
    }





}
